#ifndef __MC33879_H
#define __MC33879_H

#include "main.h"

typedef struct
{
    uint8_t chan;

    SPI_HandleTypeDef *spix;
    GPIO_TypeDef *gpio_cs;
    uint16_t gpio_pin_cs;
    GPIO_PinState cs_valid;

    GPIO_TypeDef *gpio_en;
    uint16_t gpio_pin_en;
    GPIO_PinState en_valid;

    union
    {
        struct
        {
            uint16_t io : 8;
            uint16_t open_circuit : 8;
        };
        uint16_t data;
    } out;

    union
    {
        struct
        {
            uint16_t state : 8;
            uint16_t res : 8;
        };
        uint16_t data;
    } fault;

    uint8_t err;

} t_mc33879, *p_mc33879;

extern t_mc33879 mc33879_dev[2];

void mc33879_init(p_mc33879 dev, uint8_t chan, SPI_HandleTypeDef *spix, GPIO_TypeDef *gpio_cs, uint16_t gpio_pin_cs, GPIO_PinState cs_valid, GPIO_TypeDef *gpio_en, uint16_t gpio_pin_en, GPIO_PinState en_valid);
int mc33879_rw(p_mc33879 dev, uint16_t txdata);

int test_mc33879_rw(p_mc33879 dev);

#endif
